/**
 * 
 */
package com.googlecode.pcapj;

import java.io.IOException;

import org.bridj.Pointer;

import com.googlecode.pcapj.bridj.pcap_pkthdr;

/**
 * 
 */
class CapturedWriterImpl implements CapturedWriter{
	private PcapWrapper pcapWrapper;
	private Pointer<?> dumpFilePointer;
	private Pointer<Pointer<pcap_pkthdr>> pHeader;
	private Pointer<Pointer<Byte>> pData;
	private Captured captured;
	
	/**
	 * @param pcapWrapper
	 * @param captured
	 * @param header
	 * @param pkt_data
	 */
	CapturedWriterImpl(
			PcapWrapper pcapWrapper,
			Captured captured,
			Pointer<?> dumpFilePointer,
			Pointer<Pointer<pcap_pkthdr>> pHeader, 
			Pointer<Pointer<Byte>> pData) {
		super();
		this.pcapWrapper = pcapWrapper;
		this.captured = captured;
		this.dumpFilePointer = dumpFilePointer;
		this.pHeader = pHeader;
		this.pData = pData;
	}
	/* (non-Javadoc)
	 * @see com.googlecode.pcapj.Captured#getTimeStamp()
	 */
	public TimeStamp getTimeStamp() {
		return captured.getTimeStamp();
	}
	/* (non-Javadoc)
	 * @see com.googlecode.pcapj.Captured#getCaputuredLength()
	 */
	public int getCaputuredLength() {
		return captured.getCaputuredLength();
	}
	/* (non-Javadoc)
	 * @see com.googlecode.pcapj.Captured#getLength()
	 */
	public int getLength() {
		return captured.getLength();
	}
	/* (non-Javadoc)
	 * @see com.googlecode.pcapj.Captured#getPacket()
	 */
	public Packet getPacket() {
		return captured.getPacket();
	}
	/* (non-Javadoc)
	 * @see com.googlecode.pcapj.CapturedWriter#write()
	 */
	public void write() throws IOException {
		if(pHeader == Pointer.NULL){
			throw new IOException("Captured is aleady written.");
		}
		pcapWrapper.writeDumpFile(dumpFilePointer, pHeader.get(), pData.get());
		pHeader.release();
		pHeader = null;
		pData.release();
		pData = null;
	}
}
